package com.quiz.utils;

import java.util.ArrayList;

import com.quiz.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomItemAdapter extends ArrayAdapter<CustomItem>{

	Context context;
	private int layoutResourceId;
	private ArrayList<CustomItem> objects;
	
	// objects is the list of objects we want to display
	public CustomItemAdapter(Context context, int layoutResourceId, ArrayList<CustomItem> objects) {
		super(context, layoutResourceId, objects);
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.objects = objects;
		
	}
	
	@Override
	public View getView (int position, View convertView, ViewGroup parent) {
		
		View v = convertView;
		if (v ==  null) {
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(layoutResourceId, parent, false);
		}

		
		CustomItem i = objects.get(position);
		if (i != null) {
			
			ImageView icon = (ImageView) v.findViewById(R.id.custom_item_icon);
			TextView text = (TextView) v.findViewById(R.id.custom_item_text);
			if (icon != null) {
				icon.setImageResource(i.getIconId());
			}
			if (text != null) {
				if(i.getTextId() != 0) {
					text.setText(i.getTextId());
				}
				
			}
			
		}
		
		return v;
	}
	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		
		return getView(position, convertView, parent);
	}
}
